/*
 *  loading.c
 *  test
 *
 *  Created by Phillip LeBlanc on 11/4/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */
#include "common.h"

Mix_Chunk *hillonemin;
Mix_Chunk *hilltensec;
Mix_Chunk *hill30sec;
Mix_Chunk *hillmoved;

Mix_Chunk *sniper30;
Mix_Chunk *sniper10;
Mix_Chunk *sniper_aval;

Mix_Chunk *rockets30;
Mix_Chunk *rockets10;
Mix_Chunk *rockets_aval;

Mix_Chunk *over30;
Mix_Chunk *over10;
Mix_Chunk *over_aval;

Mix_Chunk *camo30;
Mix_Chunk *camo10;
Mix_Chunk *camo_aval;


char *mapname[4][5] = {{ "maps/guardian_slayer.txt" , "maps/guardian_ball.txt" , "undefined.txt" , "undefined.txt" , "undefined.txt" },
					 { "maps/construct_slayer.txt" , "undefined.txt" , "maps/construct_hill.txt" , "undefined.txt" , "undefined.txt" },
					 { "maps/narrows_slayer.txt" , "undefined.txt" , "undefined.txt" , "maps/narrows_ctf15.txt" , "maps/narrows_ctf30.txt" },
					 { "maps/pit_slayer.txt" , "undefined.txt" , "maps/pit_hill.txt" , "maps/pit_ctf15.txt" , "maps/pit_ctf30.txt" }};


SDL_Surface* screen;
TTF_Font* font;

int SDL_Setup()
{
		
/***************************SDL Initializations**********************************/
        if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
                fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
                return -1;
        }

        int audio_rate = 22050;
        Uint16 audio_format = AUDIO_S16SYS;
        int audio_channels = 1;
        int audio_buffers = 4096;

        if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0) {
                fprintf(stderr, "Unable to initialize audio: %s\n", Mix_GetError());
                exit(1);
        }
		
		Uint32 flags;
		
		flags = SDL_SWSURFACE;

        screen = SDL_SetVideoMode(640, 480, 0, flags);
        if (screen == NULL) {
                fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
                return -1;
        }
		

        
/***********************************************************************************/

		
		SDL_WM_SetCaption("Team FRB Timer", NULL);
		
		if (!setBG(addpath("images/maps.jpg")))
			fprintf(stderr, "Error loading the image to the screen.");
			
		
		if (TTF_Init() == -1)  //initialize the font renderer
		{
			fprintf(stderr, "Unable to initialize SDL_ttf: %s \n", TTF_GetError());
		}
		font = TTF_OpenFont(addpath("font.ttf"), 45);
		loading();

		return 1;
}


int loadSound(Mix_Chunk **sound, const char * src)
{
	*sound = NULL;
	*sound = Mix_LoadWAV(src);
    if(*sound == NULL) {
           fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError());
		   return -1;
		   }
	return 1;
}

/* This is the loading routine for loading the map settings from
 * the configuration files and setting the coordinates
*/
int loading()
{
	coords[PIT].x = 365;
	coords[PIT].y = 276;
	coords[PIT].width = 234;
	coords[PIT].height = 121;
	
	coords[GUARDIAN].x = 35;
	coords[GUARDIAN].y = 115;
	coords[GUARDIAN].width = 234;
	coords[GUARDIAN].height = 120;
	
	coords[NARROWS].x = 365;
	coords[NARROWS].y = 115;
	coords[NARROWS].width = 234;
	coords[NARROWS].height = 120;
	
	coords[CONSTRUCT].x = 35;
	coords[CONSTRUCT].y = 276;
	coords[CONSTRUCT].width = 234;
	coords[CONSTRUCT].height = 120;
	
	//Gametype Button Coordinates Loading
	gtype_coords[CTF15].x = 370;
	gtype_coords[CTF15].y = 216;
	gtype_coords[CTF15].width = 82;
	gtype_coords[CTF15].height = 82;
	
	gtype_coords[CTF30].x = 459;
	gtype_coords[CTF30].y = 216;
	gtype_coords[CTF30].width = 82;
	gtype_coords[CTF30].height = 82;
	
	gtype_coords[SLAYER].x = 100;
	gtype_coords[SLAYER].y = 216;
	gtype_coords[SLAYER].width = 82;
	gtype_coords[SLAYER].height = 82;
	
	gtype_coords[HILL].x = 278;
	gtype_coords[HILL].y = 216;
	gtype_coords[HILL].width = 82;
	gtype_coords[HILL].height = 82;
	
	gtype_coords[BALL].x = 188;
	gtype_coords[BALL].y = 216;
	gtype_coords[BALL].width = 82;
	gtype_coords[BALL].height = 82;
	
	/**************************Load all of the sounds**************************/
	
	if (loadSound(&hillonemin, addpath("sounds/hillonemin.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
	if (loadSound(&hilltensec, addpath("sounds/hilltensec.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
	if (loadSound(&hill30sec, addpath("sounds/hill30sec.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
	if (loadSound(&hillmoved, addpath("sounds/hillmoved.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
		
	if (loadSound(&sniper30, addpath("sounds/sniper30.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
	if (loadSound(&sniper10, addpath("sounds/sniper10.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
	if (loadSound(&sniper_aval, addpath("sounds/sniper_aval.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
			
	if (loadSound(&rockets30, addpath("sounds/rockets30.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
	if (loadSound(&rockets10, addpath("sounds/rockets10.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
	if (loadSound(&rockets_aval, addpath("sounds/rockets_aval.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
		
	if (loadSound(&over30, addpath("sounds/over30.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
	if (loadSound(&over10, addpath("sounds/over10.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
	if (loadSound(&over_aval, addpath("sounds/over_aval.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());

	if (loadSound(&camo30, addpath("sounds/camo30.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
	if (loadSound(&camo10, addpath("sounds/camo10.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());
	if (loadSound(&camo_aval, addpath("sounds/camo_aval.wav"))==-1)
		fprintf(stderr, "Error when opening sound file, %s", SDL_GetError());

	
	return 1;
	
}
